package com.hitnet.hitnet.pic;


import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hitnet.hitnet.R;

import java.util.ArrayList;
import java.util.List;

public class ShowImageActivity extends Activity implements OnScrollListener
{
	private ImageView back;
	private GridView mGridView;
	private List<String> list; //
	private String dirName;
	private ChildAdapter adapter;
	private TextView dirNameText; //
	private Button complete; //
	private Handler handler = new Handler()
	{
		public void handleMessage(Message msg)
		{
			if (msg.what == -1)
			{
				Toast.makeText(ShowImageActivity.this, "show", Toast.LENGTH_LONG).show();
			}
		}

		;
	};

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.show_image_activity);
		NativeImageLoader.getInstance().setFirst(); //
		back = (ImageView)this.findViewById(R.id.comeBack);
		back.setOnClickListener(new back());
		list = getIntent().getStringArrayListExtra("data");
		dirName = getIntent().getStringExtra("dirName");
		findViews();
		adapter = new ChildAdapter(this, list, mGridView, handler, complete,dirNameText);
		mGridView.setAdapter(adapter);
		mGridView.setOnScrollListener(this);
		complete.setOnClickListener(new OnClickListener()
		{

			public void onClick(View arg0)
			{
				Intent intent = new Intent();
				intent.putStringArrayListExtra("selectedpath", (ArrayList) NativeImageLoader.mSelectList);
				setResult(Activity.RESULT_OK,intent);
				ShowImageActivity.this.finish();
						
			}
		});
	}

	class back implements OnClickListener{

		public void onClick(View arg0) {
			ShowImageActivity.this.finish();
		}
		
	}

	private void findViews()
	{
		mGridView = (GridView) findViewById(R.id.child_grid);
		dirNameText = (TextView) findViewById(R.id.dirTitle);
		complete = (Button) findViewById(R.id.complete);
		dirNameText.setText(dirName);
	}

	public void onScrollStateChanged(AbsListView view, int scrollState) {
		switch (scrollState) {
		case OnScrollListener.SCROLL_STATE_FLING:
			NativeImageLoader.getInstance().lock();
			break;
		case OnScrollListener.SCROLL_STATE_IDLE:
			int start = mGridView.getFirstVisiblePosition();
			int end = mGridView.getLastVisiblePosition();
			if (end >= mGridView.getCount()) {
				end = mGridView.getCount() - 1;
			}
			NativeImageLoader.getInstance().unlock();
			NativeImageLoader.getInstance().setLoadLimit(start, end);
			break;
		case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
			NativeImageLoader.getInstance().lock();
			break;
		default:
			break;
		}
		
	}

	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		
	}

}
